package com.newblog.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import com.newblog.model.Comment;

public class CommentDAOMysql implements CommentDAO {
	private static final String ADDCOMMENT="INSERT INTO nb_comments(comment_title,comment_content,comment_ip,comment_uid,comment_bid) values(?,?,?,?,?)";
	private static final String DELETECOMMENT="DELETE FORM nb_comments WHERE comment_id=?";
	private static final String UPDATECOMMENT="UPDATE nb_comments set comment_content=?";
	private static final String GETCOMMENT="SELECT * FROM nb_comment WHERE comment_id=?";
	private static final String GETCOMMENTLIST="SELECT * FROM nb_comment";
    private Connection conn;
    private BlogFactory factory;
	
    public CommentDAOMysql(Connection conn){
		this.conn = conn;
	}
	
    public CommentDAOMysql(BlogFactory factory){
		this.factory = factory;
	}
	
	private Comment  fillComment(ResultSet rs){
		Comment comment = new Comment();
		try{
		 comment.setComment_id(rs.getInt("comment_id"));
		 comment.setComment_title(rs.getString("comment_title"));
		 comment.setComment_content(rs.getString("comment_content"));
		 comment.setComment_ip(rs.getString("comment_ip"));
		 comment.setComment_uid(rs.getInt("comment_uid"));
		 comment.setComment_bid(rs.getInt("comment_bid"));
	    }
		catch(SQLException e){
			e.printStackTrace();
			
		}
		return comment;
	}
	
	public boolean addComment(Comment c) {
		PreparedStatement pstmt = null;
		boolean isSuccess = false;
		try{
			pstmt = conn.prepareStatement(ADDCOMMENT);
			pstmt.setString(1, c.getComment_title());
			pstmt.setString(2, c.getComment_content());
			pstmt.setString(3,c.getComment_ip());
			pstmt.setInt(4, c.getComment_uid());
			pstmt.setInt(5, c.getComment_bid());
			isSuccess = pstmt.executeUpdate()>0?true:false;
		}
		catch(SQLException e){
			e.printStackTrace();
		}
		
		return isSuccess;
	}

	public boolean deleteComment(Comment c) {
		PreparedStatement pstmt = null;
		boolean isSuccess = false;
		
		try{
		   pstmt = conn.prepareStatement(DELETECOMMENT);
		   pstmt.setInt(1, c.getComment_id());
		   isSuccess = pstmt.executeUpdate()>0?true:false;
			
		}
		catch(SQLException e){
			e.printStackTrace();
		}
		return isSuccess;
	}
    
	
	public Comment getComment(Comment c) {
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		Comment comment = null;
		try{
			pstmt = conn.prepareStatement(GETCOMMENT);
			pstmt.setInt(1, c.getComment_id());
			rs = pstmt.executeQuery();
			while(rs.next()){
				comment = this.fillComment(rs);
			}
		}
		catch(SQLException e){
			e.printStackTrace();
		}		
		return comment;
	}

	public List getCommentList() {
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List list = new LinkedList();
		try{
			pstmt = conn.prepareStatement(GETCOMMENT);
			rs = pstmt.executeQuery();
			while(rs.next()){
				list.add(this.fillComment(rs));
			}
		}
		catch(SQLException e){
			e.printStackTrace();
		}
		
		return list;
	}

	public boolean updateComment(Comment c) {
		// TODO Auto-generated method stub
		return false;
	}

}
